package com.longmao.controller;

import com.longmao.model.Owner;
import com.longmao.model.Pet;
import com.longmao.model.PetType;
import com.longmao.service.OwnerService;
import com.longmao.service.PetService;
import com.longmao.service.PetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Controller
public class PetController {
    @Autowired
    private PetService petService;
    @Autowired
    private PetTypeService petTypeService;
    @Autowired
    private OwnerService ownerService;

    @RequestMapping("/getAllPet")
    public String getAllPet(Model model) {
        List<Pet> allPet = petService.getAllPet();
        model.addAttribute("petList", allPet);
        return "index";
    }

    @RequestMapping("/toAdd")
    public String toAdd(Model model) {
        List<PetType> allPetType = petTypeService.getAllPetType();
        List<Owner> allOwner = ownerService.getAllOwner();
        model.addAttribute("types", allPetType);
        model.addAttribute("owners", allOwner);
        return "add";
    }

    @RequestMapping("/Add")
    private String Add(Pet pet) {
        System.out.println(pet);
        petService.addPet(pet);
        return "redirect:/getAllPet";
    }

    @RequestMapping("/toEdit/{id}")
    public String toEdit(@PathVariable("id") int id, Model model) {
        List<PetType> allPetType = petTypeService.getAllPetType();
        Pet pet = petService.getPetById(id);
        List<Owner> allOwner = ownerService.getAllOwner();
        model.addAttribute("types", allPetType);
        model.addAttribute("owners", allOwner);
        model.addAttribute("pt", pet);
        return "update";
    }

    @RequestMapping("/Edit")
    public String Edit(Pet pet) {
        petService.updatePet(pet);
        return "redirect:/getAllPet";
    }

    @RequestMapping("/delPet/{id}")
    public String delete(@PathVariable("id") int id) {
        petService.deletePet(id);
        return "redirect:/getAllPet";
    }


}
